package com.karl.mytask_2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.tools.BaseActivity;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.ResetPasswordByEmailListener;

/**
 * Created by Karl on 2016/5/21.
 * 重置密码
 */
public class ResetPasswordActivity extends BaseActivity implements View.OnClickListener {
    private TextView mToolBarTitle;
    private ImageButton mBack;

    private EditText mEmail;
    private Button mReset;

    String email;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.reset_password);
        initView();
        initEvent();


    }

    private void initView(){
        mToolBarTitle = (TextView) findViewById(R.id.tool_bar2_title);
        mBack = (ImageButton) findViewById(R.id.tool_bar2_back);

        mEmail = (EditText) findViewById(R.id.reset_email);
        mReset = (Button) findViewById(R.id.reset_btn);


    }

    private void initEvent(){
        mToolBarTitle.setText("重置密码");
        mBack.setOnClickListener(this);
        mReset.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.reset_btn:
                Toast.makeText(ResetPasswordActivity.this, "邮件发送中，请稍等", Toast.LENGTH_SHORT).show();
                toReset();
                break;
            case R.id.tool_bar2_back:
                ResetPasswordActivity.this.finish();
                break;
        }

    }

    private void toReset(){
        email = mEmail.getText().toString();
        if (email.equals("")){
            Toast.makeText(ResetPasswordActivity.this, "请输入邮箱地址！", Toast.LENGTH_SHORT).show();
        }else {
            //重置操作
            BmobUser.resetPasswordByEmail(this, email, new ResetPasswordByEmailListener() {
                @Override
                public void onSuccess() {
                    //发送邮件成功
                    startActivity(new Intent().setClass(ResetPasswordActivity.this,ResetSuccessActivity.class));
                    ResetPasswordActivity.this.finish();
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(ResetPasswordActivity.this, "邮件发送失败！ "+s, Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

}
